package com.eternal.log.adapter;

import androidx.paging.PagedListAdapter;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;

import android.util.SparseArray;
import android.view.ViewGroup;

import com.eternal.base.concat.LogExtra;
import com.eternal.base.database.entity.Log;
import com.eternal.log.R;

import java.util.Calendar;

public class LogCAdapter extends PagedListAdapter<Log, LogCHolder> {
    private static final int CONTENT = 0;
    private static final int HEADER = 1;
    private static final int FOOBAR = 2;
    private static final int FOOBAR_HEADER = 3;

    private long start;
    private boolean isDegree;
    private Calendar first, last;
    private SparseArray<String> notifyCache;
    private long end;

    public LogCAdapter(LogExtra extra) {
        super(diff);
        first = Calendar.getInstance();
        last = Calendar.getInstance();
        setExtra(extra);
    }

    public void setExtra(LogExtra extra) {
        if (isDegree != extra.isDegree) {
            isDegree = extra.isDegree;
            notifyItemRangeChanged(0, getItemCount());
        }
        notifyCache = extra.notifyName;
        if (extra.time == 0) {
            start = System.currentTimeMillis();
        } else {
            start = extra.time;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            end = getItem(position).time;
            return HEADER;
        } else if (position == getItemCount() - 1) {
            return FOOBAR;
        }
        first.setTimeInMillis(getItem(position).time);
        last.setTimeInMillis(getItem(position - 1).time);
        if (first.get(Calendar.DAY_OF_MONTH) != last.get(Calendar.DAY_OF_MONTH) ||
                first.get(Calendar.MONTH) != last.get(Calendar.MONTH) ||
                first.get(Calendar.YEAR) != last.get(Calendar.YEAR)) {
            return FOOBAR_HEADER;
        } else {
            return CONTENT;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @NonNull
    @Override
    public LogCHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        switch (i) {
            case HEADER:
                return new LogCHeaderHolder(viewGroup);
            case FOOBAR:
                return new LogCHolder(viewGroup, R.layout.log_item_foobar);
            case FOOBAR_HEADER:
                return new LogCHeaderHolder(viewGroup, R.layout.log_item_foobar_header);
            default:
                return new LogCHolder(viewGroup);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull LogCHolder viewHolder, int i) {
        viewHolder.init(getItem(i), start, isDegree);
    }

    public boolean isClear() {
        return start >= end;
    }

    public void clear() {
        this.start = System.currentTimeMillis();
        notifyItemRangeChanged(0, getItemCount());
    }

    private static DiffUtil.ItemCallback<Log> diff = new DiffUtil.ItemCallback<Log>() {
        @Override
        public boolean areItemsTheSame(@NonNull Log log, @NonNull Log t1) {
            return log.id == t1.id && log.time == t1.time;
        }

        @Override
        public boolean areContentsTheSame(@NonNull Log log, @NonNull Log t1) {
            return log.id == t1.id && log.time == t1.time;
        }
    };
}
